﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace sy_dll_inject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_btnOk.PerformClick();
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            // 若当前是选择进程状态，更新当前进程数组
            if(this.m_radiobtnSelect.Checked)
            {
                GetAllActiveProcess();                      // 更新进程信息
                ShowAllActiveProcess(ref this.comboBox1);   // 显示进程信息（添加到下拉列表中）
            }
            else
            {

            }
        }

        private void m_btnBeginInject_Click(object sender, EventArgs e)
        {
            if(selectedProcess != null && m_listCurDllFile.Count != 0)
            {
                foreach(string file in m_listCurDllFile)
                {
                    if (ProcessUtils.DoInjectDllToProcess(selectedProcess, file))
                    {
                        MessageBox.Show("注入成功！");
                    }
                }
            }
            else
            {
                if(selectedProcess == null)
                {
                    MessageBox.Show("没有选择进程！！！");
                }
                if(m_listCurDllFile.Count == 0)
                {
                    MessageBox.Show("没有添加需要注入的 dll 文件");
                }
            }
        }

        private void m_btnAdvancedSetting_Click(object sender, EventArgs e)
        {
            MessageBox.Show("功能尚未开发，敬请期待！");
        }

        private void m_btnOk_Click(object sender, EventArgs e)
        {
            if (this.m_radiobtnSelect.Checked)
            {
                if (this.comboBox1.SelectedItem != null)
                {
                    this.selectedProcess = ((ComboboxItem)this.comboBox1.SelectedItem).Value;
                }
            }
            else
            {
                MessageBox.Show("功能尚未开发，敬请期待！");
            }
        }

        private void m_btnAdd_Click(object sender, EventArgs e)
        {
            AddDllFile();
            ShowAllDllFileOnListView(ref this.listView1);
        }

        private void m_btnDeleteSelected_Click(object sender, EventArgs e)
        {
            DeleteSelectListViewItem(ref this.listView1);
        }

        private void m_btnDeleteAll_Click(object sender, EventArgs e)
        {
            m_listCurDllFile.Clear();
            ShowAllDllFileOnListView(ref this.listView1);
        }

        private void m_btnReFlush_Click(object sender, EventArgs e)
        {
            ShowAllDllFileOnListView(ref this.listView1);
        }

        private void m_radiobtnSelect_CheckedChanged(object sender, EventArgs e)
        {
            this.comboBox1.Text = "";
        }

        private void m_radiobtnCreate_CheckedChanged(object sender, EventArgs e)
        {
            this.comboBox1.Text = "";
            this.comboBox1.Items.Clear();
            m_arrayCurProcess = null;
        }
    }
}
